package com.cnunicom.modules.basisdata.service.impl;

import com.cnunicom.modules.sys.entity.SysUserEntity;
import com.cnunicom.modules.sys.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.Query;

import com.cnunicom.modules.basisdata.dao.BasisDataDao;
import com.cnunicom.modules.basisdata.entity.BasisDataEntity;
import com.cnunicom.modules.basisdata.service.BasisDataService;

import javax.annotation.Resource;


@Service("basisDataService")
public class BasisDataServiceImpl extends ServiceImpl<BasisDataDao, BasisDataEntity> implements BasisDataService {


    @Resource
    private SysRoleService sysRoleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws ParseException {

        BasisDataEntity entity = getBasisDataEntity(params);

        IPage<BasisDataEntity> page = this.page(
                new Query<BasisDataEntity>().getPage(params),
                new QueryWrapper<BasisDataEntity>()
                        .setEntity(entity)
                        .between(StringUtils.isNotBlank(entity.getCreateStartTime()),"create_time",entity.getCreateStartTime(),entity.getCreateEndTime())
                        .between(StringUtils.isNotBlank(entity.getUpdateStartTime()),"update_time",entity.getUpdateStartTime(),entity.getUpdateEndTime())
        );

        return new PageUtils(page);
    }

    private BasisDataEntity getBasisDataEntity(Map<String, Object> params) throws ParseException {

        BasisDataEntity entity = new BasisDataEntity();

        SysUserEntity user = (SysUserEntity) params.get("user");
        Long userId = user.getUserId();
        boolean isReader = SecurityUtils.getSubject().hasRole("作者");

        String createUserName = (String) params.get("createUserName");
        String articleTitle = (String) params.get("articleTitle");
        String createTime = (String) params.get("createTime");
        String updateTime = (String) params.get("updateTime");
        String typeStr = (String) params.get("articleType");
        Integer articleType = StringUtils.isNotBlank(typeStr) ? Integer.parseInt(typeStr) : null;
        entity.setIsDelete(0);
        entity.setArticleType(articleType);

        //是作者,查询条件带id
        if (isReader) {
            entity.setCreateUserId(userId);

        }
        if (StringUtils.isNotBlank(createUserName)) {
            entity.setCreateUserName(createUserName);
        }

        if (StringUtils.isNotBlank(articleTitle)) {
            entity.setArticleTitle(articleTitle);
        }

        if (StringUtils.isNotBlank(createTime)) {
            String[] createTimeSplit = createTime.split(",");
            entity.setCreateStartTime(createTimeSplit[0]);
            entity.setCreateEndTime(createTimeSplit[1].concat(" 23:59:59"));
        }

        if (StringUtils.isNotBlank(updateTime)) {
            String[] updateTimeSplit = updateTime.split(",");
            entity.setUpdateStartTime(updateTimeSplit[0]);
            entity.setUpdateEndTime(updateTimeSplit[1].concat(" 23:59:59"));
        }
        return entity;
    }
}
